package com.haopt.clock.service.impl;

import com.haopt.clock.dao.TClassificationMapper;
import com.haopt.clock.dto.ResultDTO;
import com.haopt.clock.entity.TClassification;
import com.haopt.clock.service.ClassificationService;
import com.haopt.clock.utils.BeanHelper;
import com.haopt.clock.utils.IdBuilderUtil;
import com.haopt.clock.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author haopt
 * @version 1.0
 * @ClassName ClassificationServiceImpl
 * @Description TODO
 * @date 2021/3/19 16:01
 */
@Service
public class ClassificationServiceImpl implements ClassificationService {
    @Autowired
    private TClassificationMapper classificationMapper;
    @Override
    public ResultDTO<List<ResClassificationVO>> list() {
        List<TClassification> resultList = classificationMapper.selectAll();
        return ResultDTO.okOf(BeanHelper.copyWithCollection(resultList, ResClassificationVO.class));
    }

    @Override
    public ResultDTO<ResIdVO> add(ReqClassificationAddVO vo) {
        TClassification classification = BeanHelper.copyProperties(vo, TClassification.class);
        classification.setDeleted(1);
        classification.setId(IdBuilderUtil.getId());
        classification.setCreateTime(System.currentTimeMillis());
        classification.setUpdateTime(System.currentTimeMillis());
        classificationMapper.insertSelective(classification);
        return ResultDTO.okOf(new ResIdVO(classification.getId()));
    }

    @Override
    public ResultDTO update(ReqClassificationUpdateVO vo) {
        TClassification classification = BeanHelper.copyProperties(vo, TClassification.class);
        classification.setUpdateTime(System.currentTimeMillis());
        classificationMapper.updateByPrimaryKeySelective(classification);
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO delete(ReqIdVO vo) {
        classificationMapper.deleteByPrimaryKey(vo.getId());
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO<ResClassificationDetailVO> detail(String id) {
        TClassification classification = classificationMapper.selectByPrimaryKey(id);
        ResClassificationDetailVO resClassificationDetailVO = BeanHelper.copyProperties(classification, ResClassificationDetailVO.class);
        return ResultDTO.okOf(resClassificationDetailVO);
    }
}
